//
//  YanZhengPhoneVController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "YanZhengPhoneVController.h"
#import "NSString+Verify.h"
#import "XiuGaiMiMaViewController.h"
#import "SetPayPasswordVController.h"
@interface YanZhengPhoneVController ()
{
    NSString *phoneStr;
    
    NSString *sms_code;
}

@end

@implementation YanZhengPhoneVController

- (void)viewDidLoad {
    [super viewDidLoad];
    _yanZBtn.hidden = NO;
    _timeLB.hidden = YES;
    _phoneTF.userInteractionEnabled = NO;
    
    NSRange range1 = {0,4};
    NSRange range2 = {8,3};
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    phoneStr = saveDic[@"phonenumber"];
    NSString *onestr = [phoneStr substringWithRange:range1];
    NSString *twoStr = [phoneStr substringWithRange:range2];
    _phoneTF.text = [NSString stringWithFormat:@"%@****%@",onestr,twoStr];
    
    
    [self configureNaBar];
}

- (void)configureNaBar {
        self.navigationItem.title = @"验证绑定手机";
        //设置导航栏按钮的边距
        UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame= CGRectMake(0, 0, 60, 30);
        [rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
        [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        /**
         width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
         */
        negativeSpacerleft.width = -20;
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
        
        
        
    }
    
    - (void)handleBackAxtion {
        [self.navigationController popViewControllerAnimated:YES];
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//验证码
- (IBAction)yanZengBtnACtion:(UIButton *)sender {
    
    if (![phoneStr verifyTelephone]) {
        
        [self.view showTextNoActivity:@"手机格式不正确" timeLength:1];
        return;
    }
    
    [self getCodeStr];
    

}


// 获取验证码
-(void)getCodeStr{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"mobile":phoneStr,@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    if ([phoneStr verifyTelephone]) {
        [HttpRequest postWithURLString:POST(Shop_GetSmsCodeUrl) parameters:parameDic success:^(id responseObject) {
            
            NSLog(@"%@",responseObject);
            
            if ([[responseObject objectForKey:@"ret"] integerValue] == 200) {
                NSDictionary *dataDIc = [responseObject objectForKey:@"data"];
                sms_code = [NSString stringWithFormat:@"%@",[dataDIc objectForKey:@"sms_code"]];
                
                [self timeOutAction];
                
            }else{
                [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"msg"]]];
            }
            
            
        } failure:^(NSError *error) {
            
        }];
    }
    
}
-(void)timeOutAction{
    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                _yanZBtn.hidden = NO;
                _yanZBtn.selected = YES;
                _timeLB.hidden = YES;;
                
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2dS", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                
                _yanZBtn.hidden = YES;
                _timeLB.hidden = NO;
                _timeLB.text = strTime;
                [UIView commitAnimations];
                
                
            });
            timeout--;
        }
    });
    
    dispatch_resume(_timer);
}

//确定
- (IBAction)sureBtnAction:(UIButton *)sender {
    
    if ([sms_code isEqualToString:_yanZTF.text]) {
        [[DSLHUD shareDSLHUD] showHUDWithLable:@"验证成功" SuperView:self.view FontSize:14 HudDismissTiem:1 HudDismissBlock:^{
            
            if ([_pushTypeStr isEqualToString:@"修改密码"]) {
                XiuGaiMiMaViewController *fix = [[XiuGaiMiMaViewController alloc] init];
                [self.navigationController pushViewController:fix animated:YES];
            }
            
            if ([_pushTypeStr isEqualToString:@"支付密码"]) {
                SetPayPasswordVController *se = [[SetPayPasswordVController alloc] init];
                se.typeStr = @"支付密码";
                se.phoneStr = phoneStr;
                se.codeStr = sms_code;
                [self.navigationController pushViewController:se animated:YES];
            }
            
            if ([_pushTypeStr isEqualToString:@"提现密码"]) {
                SetPayPasswordVController *se = [[SetPayPasswordVController alloc] init];
                se.typeStr = @"提现密码";
                [self.navigationController pushViewController:se animated:YES];
            }
            
            
            
        }];
    }else{
    
        [MBProgressHUD showText:@"验证码错误"];
    }
    
    
}

@end
